import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * Created by zhangcheng on 2017/4/20 0020.
 */
public class SinaCrawler implements PageProcessor {


    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);

    static int i = 0;
    static int depth = 0;
    static long start = System.currentTimeMillis();

    public void process(Page page) {
//        if (depth != 20)
//            page.addTargetRequests(page.getHtml().links().all());
//        depth ++;
        long end = System.currentTimeMillis();
        System.out.println(page.getHtml());
        System.out.println("爬取的第" + ++i + "个网页。耗时："+(end-start)/1000+"秒");
    }

    public Site getSite() {
        return this.site = site;
    }

    public static void main(String[] args) throws Exception{
        Spider.create(new SinaCrawler()).addUrl("http://www.sina.com.cn/").thread(2).run();
    }
}
